using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace FiltronASP
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.Add("DefaultCMS",
                       new Route("{pageAlias},{pageId}.html/",
                                    new RouteValueDictionary(
                                        new
                                        {
                                            controller = "CMS",
                                            action = "Index",
                                            pageId = -1,
                                            pageAlias = "main-page"
                                        }),
                                    new RouteValueDictionary(new { pageId = @"\d+" }),
                                    new MvcRouteHandler()));

            routes.MapRoute(
                "StartPage", // Route name
                "{controller}/{action}/{pageAlias}/{pageId}", // URL with parameters
                new
                {
                    controller = "CMS",
                    action = "Index",
                    pageAlias = "main-page",
                    pageId = "-1"
                }
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }

        public override string GetVaryByCustomString(HttpContext context, string arg)
        {
            if (String.IsNullOrEmpty(arg))
            {
                return base.GetVaryByCustomString(context, arg);
            }   //if
            if (arg == "browserLang")
            {
                if (context.Request.UserLanguages != null)
                {
                    string lang = Request.UserLanguages[0];
                    if (lang != null)
                    {
                        if (!lang.StartsWith("tr"))
                        {
                            if (lang.Length > 2)
                            {
                                lang = lang.Substring(0, 2);
                            }   //if
                            return lang;
                        }   //if
                    }   //if
                }   //if
                return "en";
            }   //if
            return base.GetVaryByCustomString(context, arg);
        }
 
    }
}